package org.axed.user.client;

import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.Window;

/**
 * A GWT workaround for firefox.
 */
public class DOM extends com.google.gwt.user.client.DOM {

	static boolean haveClientRect = false;

	static {
		haveClientRect = testClientRect();
	}

	private static native boolean testClientRect() /*-{
		return typeof ($doc.body.getBoundingClientRect) != "undefined";
	}-*/;
        
    private static native int getALeft(Element e) /*-{
		return Math.round(e.getBoundingClientRect().left);
    }-*/;
	
    private static native int getATop(Element e) /*-{
       return Math.round(e.getBoundingClientRect().top);
    }-*/;

	public static int getAbsoluteLeft(Element e) {
		if (haveClientRect) {
			return getALeft(e) + Window.getScrollLeft();
		} else {
			return com.google.gwt.user.client.DOM.getAbsoluteLeft(e);
		}
	}

	public static int getAbsoluteTop(Element e) {
		if (haveClientRect) {
			return getATop(e) + Window.getScrollTop();
		} else {
			return com.google.gwt.user.client.DOM.getAbsoluteTop(e);
		}
	}
}
